package com.jiesen.base.modules.certificate.textbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiesen.base.modules.certificate.textbook.model.JsVideo;
import com.jiesen.base.modules.certificate.textbook.service.JsVideoService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiesen.base.baseDao.BaseServiceImpl;
import com.jiesen.base.modules.certificate.textbook.mapper.TextbookMapper;
import com.jiesen.base.modules.certificate.textbook.model.TextBook;
import com.jiesen.base.modules.certificate.textbook.service.TextbookService;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bao
 */
@Service
public class TextBookServiceImpl extends BaseServiceImpl<TextbookMapper, TextBook>implements TextbookService {

    @Autowired
    private JsVideoService jsVideoService;

    @Override
    public List<TextBook> getTextbookByParentId(Integer parentId) {
        QueryWrapper<TextBook> queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",parentId);
        queryWrapper.orderByDesc("sorts");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<TextBook> getTextBookAndChildById(Integer id) {
        List<TextBook> textBooks = getTextbookByParentId(id);
        if(textBooks.size()<=0){
            return null;
        }
        for (TextBook textBook : textBooks) {
            textBook.setChild(getTextBookAndChildById(textBook.getId()));
        }
        return textBooks;
    }

    //删除一个证书文件夹,并删除其下的所有文件夹和视频文件
    @Override
    @Transactional
    public boolean removeById(Serializable id) {
        QueryWrapper<TextBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<TextBook> textBooks = baseMapper.selectList(queryWrapper);
        if(textBooks==null||textBooks.size()==0){
            jsVideoService.deleteByBookId(id);
        }else{
            for (TextBook textBook : textBooks) {
                removeById(textBook.getId());
            }
        }

        return super.removeById(id);
    }
}
